package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Coupon;
import com.tbit.uqbike.object.pojo.NewUserActivity;
import com.tbit.uqbike.service.business.NewUserActivityService;
import com.tbit.uqbike.webmanager.dao.core.CouponDao;
import com.tbit.uqbike.webmanager.dao.core.NewUserActivityDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 新用户赠送
 * @Date: 创建于 14:01 2020/6/5
 */
@Service
public class NewUserActivityServiceImpl implements NewUserActivityService {
    @Autowired
    private NewUserActivityDao newUserActivityDao;
    @Autowired
    private CouponDao couponDao;

    @Override
    public List<NewUserActivity> queryPage(Integer accountId, Integer pageNO, Integer rowCount) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("pageNO", (pageNO - 1) * rowCount);
        map.put("rowCount", rowCount);
        return newUserActivityDao.queryPage(map);
    }

    @Override
    public Integer queryCount(Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        return newUserActivityDao.queryCount(map);
    }

    @Override
    public void insert(NewUserActivity newUserActivity) {
        if (newUserActivity.getCouponId() != null) {
            Coupon coupon = couponDao.getByCouponId(newUserActivity.getCouponId());
            if (coupon == null) {
                throw new BaseException("coupon_not_exit");
            }
        }
        NewUserActivity newUserActivityDB = newUserActivityDao.getByAccountId(newUserActivity.getAccountId());
        if (newUserActivityDB != null) {
            throw new BaseException("ver.account.exist.activity");
        }
        newUserActivityDao.insert(newUserActivity);
    }

    @Override
    public void edit(NewUserActivity newUserActivity) {
        if (newUserActivity.getCouponId() != null) {
            Coupon coupon = couponDao.getByCouponId(newUserActivity.getCouponId());
            if (coupon == null) {
                throw new BaseException("coupon_not_exit");
            }
        }
        newUserActivityDao.edit(newUserActivity);
    }

    @Override
    public void del(Integer newuseractivityId) {
        newUserActivityDao.del(newuseractivityId);
    }
}
